﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

using edowork.com.SQL2005;
using edowork.com.Model;

namespace WebInvoicingsys.system.officials
{
    public partial class announcement_edit : System.Web.UI.Page
    {
        //前台输出提示信息变量
        public string alert_msg = "";

        //传入的参数
        string id = "", title = "", range = "";

        /// <summary>
        /// 取得传入的参数
        /// </summary>
        /// <returns></returns>
        private bool GetParma()
        {
            try
            {
                id = Request.Params["ID"].ToString();
                title = Request.Params["title"].ToString();
                range = Request.Params["range"].ToString();
            }
            catch
            {
                //显示出错信息
                alert_msg = String.Format(Scripts.alert_warning, Errormsg.er_Parampost);
                Page.ClientScript.RegisterClientScriptBlock(this.GetType(), "alert", Scripts.alert_script, true);
                return false;
            }

            return true;
        }

        private void bind()
        {
            Execute exec = new Execute(Session["connectionString"].ToString());

            string sql = "", error_msg = "";

            //构造SQL语句
            sql = @"select ID, Title, p_date, e_date, content, userid, p_range, departmentid, sysdate, attach1, attach2, attach1filename, attach2filename,
                           (select username from sysuser where sysuser.userid=announcement.userid) username,
	                       (select departmentremark from department where department.departmentid=announcement.departmentid) departmentremark
                      from announcement where ID='{0}'";

            sql = String.Format(sql, id);

            DataSet ds = exec.ExecSQLQueryDataSet(sql, ref error_msg);

            if ((ds == null) || (ds.Tables.Count <= 0) || (ds.Tables[0].Rows.Count <= 0))
            {
                alert_msg = String.Format(Scripts.alert_warning, error_msg);
                Page.ClientScript.RegisterClientScriptBlock(this.GetType(), "alert", Scripts.alert_script, true);
                return;
            }

            foreach (DataRow dr in ds.Tables[0].Rows)
            {
                txt_Title.Text = dr["Title"].ToString();
                txt_p_date.Text = dr["p_date"].ToString();
                txt_e_date.Text = dr["e_date"].ToString();

                uc_from_notice.SetByValue(dr["p_range"].ToString());
                txt_content.Text = dr["content"].ToString();

                txt_attach1.Text = dr["attach1filename"].ToString();
                txt_attach2.Text = dr["attach2filename"].ToString();
            }
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            ;
        }

        protected void Page_PreRender(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                GetParma();

                bind();
            }
        }

        //保存
        protected void btnSave_Click(object sender, EventArgs e)
        {
            if (GetParma() == false)
            {
                return;
            }

            alert_msg = "";

            edowork.com.Model.announcementInfo announcementInfo = new announcementInfo();

            announcementInfo.Title = txt_Title.Text.Trim();
            announcementInfo.P_date = txt_p_date.Text.Trim();
            announcementInfo.E_date = txt_e_date.Text.Trim();
            announcementInfo.P_range = uc_from_notice.getValue();
            announcementInfo.Content = txt_content.Text.Trim();

            if (String.IsNullOrEmpty(announcementInfo.Title))
            {
                alert_msg = String.Format(Scripts.alert_warning, "请填写标题。");

                Page.ClientScript.RegisterClientScriptBlock(this.GetType(), "alert", Scripts.alert_script, true);

                return;
            }

            if (String.IsNullOrEmpty(announcementInfo.P_date))
            {
                alert_msg = String.Format(Scripts.alert_warning, "请填写行文日期。");

                Page.ClientScript.RegisterClientScriptBlock(this.GetType(), "alert", Scripts.alert_script, true);

                return;
            }

            if (String.IsNullOrEmpty(announcementInfo.E_date))
            {
                alert_msg = String.Format(Scripts.alert_warning, "请填写有效日期。");

                Page.ClientScript.RegisterClientScriptBlock(this.GetType(), "alert", Scripts.alert_script, true);

                return;
            }

            if (String.IsNullOrEmpty(announcementInfo.P_range))
            {
                alert_msg = String.Format(Scripts.alert_warning, "请选择通知范围。");

                Page.ClientScript.RegisterClientScriptBlock(this.GetType(), "alert", Scripts.alert_script, true);

                return;
            }

            if (String.IsNullOrEmpty(announcementInfo.Content))
            {
                alert_msg = String.Format(Scripts.alert_warning, "请填写通知内容。");

                Page.ClientScript.RegisterClientScriptBlock(this.GetType(), "alert", Scripts.alert_script, true);

                return;
            }

            //上传附件1
            if (fp_attach1.HasFile == true)
            {
                announcementInfo.Attach1 = DateTime.Now.ToString("yyMMddhhmmss") + "_" + fp_attach1.FileName;
                announcementInfo.Attach1filename = fp_attach1.FileName;

                //上传文件
                string webfilepath = Application["webfilepath"].ToString();
                fp_attach1.SaveAs(webfilepath + announcementInfo.Attach1);
            }

            //上传附件2
            if (fp_attach2.HasFile == true)
            {
                announcementInfo.Attach2 = DateTime.Now.ToString("yyMMddhhmmss") + "_" + fp_attach2.FileName;
                announcementInfo.Attach2filename = fp_attach2.FileName;

                //上传文件
                string webfilepath = Application["webfilepath"].ToString();
                fp_attach2.SaveAs(webfilepath + announcementInfo.Attach2);
            }

            //保存数据入数据库
            using (edowork.com.SQL2005.TranUtil tranutil = new TranUtil(Session["connectionString"].ToString()))
            {
                System.Data.SqlClient.SqlTransaction tran = null;

                try
                {
                    tran = tranutil.GetSqlTransaction();
                }
                catch (Exception er)
                {
                    alert_msg = String.Format(Scripts.alert_warning, er.Message);

                    Page.ClientScript.RegisterClientScriptBlock(this.GetType(), "alert", Scripts.alert_script, true);

                    return;
                }

                edowork.com.SQL2005.Execute exec = new Execute(Session["connectionString"].ToString());

                string sql = "";
                int result = -1;

                //保存入announcement表
                sql = String.Format("update announcement set Title='{1}', p_date='{2}', e_date='{3}', content='{4}', p_range='{5}', attach1='{6}', attach2='{7}', attach1filename='{8}', attach2filename='{9}' where ID={0}", id,
                                    announcementInfo.Title, announcementInfo.P_date, announcementInfo.E_date, announcementInfo.Content,
                                    announcementInfo.P_range,
                                    announcementInfo.Attach1, announcementInfo.Attach2, announcementInfo.Attach1filename, announcementInfo.Attach2filename);

                result = exec.ExecSQLQueryInt(sql, ref alert_msg, tran);

                if (result < 0)
                {
                    tran.Rollback();
                    tranutil.CloseConnection();

                    alert_msg = String.Format(Scripts.alert_warning, alert_msg);

                    Page.ClientScript.RegisterClientScriptBlock(this.GetType(), "alert", Scripts.alert_script, true);

                    return;
                }

                tran.Commit();
                tranutil.CloseConnection();
            }

            alert_msg = String.Format(Scripts.alert_success, Errormsg.success_Save);

            Page.ClientScript.RegisterClientScriptBlock(this.GetType(), "alert", Scripts.alert_script, true);
        }

        //返回
        protected void btnReturn_Click(object sender, EventArgs e)
        {
            GetParma();

            string url = "/system/officials/announcement_list.aspx?title={0}&range={1}";

            url = String.Format(url, title, range);

            Response.Redirect(url);
        }
    }
}